package com.zhuss.product.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.preline.CreatePrelineSignBo;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.bean.vo.product.preline.DetailPrelineSignVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import com.zhuss.product.db.entity.PrelineSign;
import com.zhuss.util.JsonUtil;


/**
 * @description: 招募报名创建
 * @author: zhushanshuo
 * @create: Wed Jan 06 16:26:20 CST 2021
 **/
@Component("prelineSignCreate")
public class PrelineSignCreate extends AbstractProcessor<Object, CreatePrelineSignBo> implements Processor<Object, CreatePrelineSignBo> {

    @Autowired
    private PrelineSignDao prelineSignDao;
    
    @Autowired
	@Qualifier("prelineSignRead")
	private PrelineSignRead prelineSignRead;
    
    @Autowired
    private LineRead lineRead;

    
    /**
     * 校验
     */
    @Override
    public void check(CreatePrelineSignBo bo) throws DzcxException {
    	// TODO 创建 招募报名 校验， 请补充
		DetailPrelineSignVo bean = new DetailPrelineSignVo();
		bean.setPrelineId(bo.getPrelineId());
		bean.setCreatorId(bo.getConcurrentUserId());
		DetailPrelineSignVo sign = prelineSignDao.getDetail(bean);
    	if (sign!=null) {
    		 throw new DzcxException(ExceptionCode.新线招募已报名);
    	}
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public Object process(CreatePrelineSignBo param) throws DzcxException {
    	PrelineSign record = new PrelineSign();
		DetailLineVo line = lineRead.detail(param.getPrelineId());
    	record.setPrelineId(param.getPrelineId());
    	record.setName(line.getName());
    	record.setRideType(line.getRideType());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setPrice(line.getPrice());
    	record.setSort(line.getSort());

    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	prelineSignDao.create(record);
    	param.setTargetId(record.getPrelineSignId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreatePrelineSignBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.新线报名.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
